<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="ssi.tools.Connector" %>    
<%@page import="ssi.beans.User"%>
<%@page import="ssi.beans.Message" %>
<% 
	Message[] receivedMessages = null;
	Message[] sentMessages = null;
	if(session.getAttribute("user")!=null){
		User user = (User)session.getAttribute("user");
		receivedMessages = Message.getInboxMessages(user.getId());
		sentMessages = Message.getOutboxMessages(user.getId());
	}
%>

<%
User user=(User)session.getAttribute("user");
String type=user.getTypeOfUser().toString();
Connection connection = new Connector().getConnection();
Statement statement = connection.createStatement();
ResultSet result=null;
int numberOfRows=0;
	result = statement.executeQuery("SELECT name, surname, id, email FROM users where email <>'"+user.getEmail()+"'");
	numberOfRows = Connector.getRowCount(result);
%>
<script type="text/javascript" src="js/jQuery.js"></script>
<script type="text/javascript" src="js/jquery.nivo.slider.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript" src="js/messages.js"></script>
<h2>Wiadomosci</h2>

	<div class="box">
		<div class="menuPanel">
		
			<center>
			<font color="red"><html:errors property="error" /></font>
			<div id="inbox" class="button">Skrzynka odbiorcza</div> 
			<div id="outbox" class="button">Skrzynka nadawcza</div>
			<div id="new" class="button">Nowa wiadomosc</div></center>
		</div>
		<br>
		<div id="inboxScreen" class="screen">
		<h3>Skrzynka Odbiorcza</h3>
			<% if(receivedMessages != null){ %>
			<table id="tabela">
				<tr><td><b>Lp</b></td><td><b>Nadawca</b></td><td><b>Temat</b></td><td><b>Data</b></td></tr>
				<%int i=0;  
				for(Message message : receivedMessages){ 
				i++;%>
				
				<tr><td><b><%=i %></b></td><td> <%=message.getSender()[0]+" "+message.getSender()[1] %></td> <td><%= message.getTitle() %></td><td><%=message.getSentDate() %></td><td><a href="/SSIProject/index.jsp?page=showMessage&id=<%=message.getId() %>">Otworz wiadomosc</a></td></tr>
				
				
				<% } %>
				</table>
			<% }else{ %>
			<p>Skrzynka jest pusta!</p>
			<%} %>
		</div>
		<div id="outboxScreen" class="hidden screen">
		<h3>Skrzynka Nadawcza</h3>
			<% if(sentMessages != null){ %>
			<table id="tabela">
				<tr><td><b>Lp</b></td><td><b>Odbiorca</b></td><td><b>Temat</b></td><td><b>Data</b></td></tr>
				<% int i=0; 
				for(Message message : sentMessages){ 
				i++;
				%>
				
				<tr><td><b><%=i %></b></td><td> <%=message.getReceiver()[0]+" "+message.getReceiver()[1] %></td><td> <%= message.getTitle() %></td><td><%=message.getSentDate() %></td><td><a href="/SSIProject/index.jsp?page=showSent&id=<%=message.getId() %>">Otworz wiadomosc</a></td></tr>
				
				
				<% } %>
					</table>
			<% }else{ %>
			<p>Skrzynka jest pusta!</p>
			<%} %>
		</div>
		<div id="newMessage" class="hidden screen">
		<h3>Nowa Wiadomosc</h3>
			<html:form action="sendMessage">
			<table>
			<tr>
			<td>Do:</td><td><select name="receiverId" id="receiverId">
							<option value="0">Wybierz... </option>
							<%
								int i=0;
								while(result.next()){%>
									<option value="<%=result.getString("id") %>"><%=result.getString("name")+" "+result.getString("surname") %></option>
								<% } %>
			</select></td>
			</tr>
			<tr>
			<td>Temat:</td><td><input type="text" name="title" id="title" /></td>
			</tr>
			<tr>
			<td>Tekst:</td><td> <textarea name="text" id="text" rows="9" cols="40"></textarea></td>
			</tr>
			</table>
			<center><input type="submit" class="submit" value="Wyslij" /></center>
			</html:form>
		</div>
	
	</div>



